package com.hhit.collaborative_learning.utils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.hhit.collaborative_learning.entity.SearchResult;

public class DateUtil {

	public static String formatDate(Date date, String format) {
		String result = "";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if (date != null) {
			result = sdf.format(date);
		}
		return result;
	}

	public static Date formatString(String str, String format) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(str);
	}

	public static String formateTimestamp(Date time1, Date time2) {
		long relong = time1.getTime() - time2.getTime();
		if (relong > 0) {
			long s = relong / 1000;
			long m = relong / (1000 * 60);
			long h = relong / (1000 * 60 * 60);
			long d = relong / (1000 * 60 * 60 * 24);
			long z = relong / (1000 * 60 * 60 * 24 * 7);
			if (m <= 0) {
				return s + "秒 前";
			} else {
				if (m < 60) {
					return m + "分钟 前";
				} else {
					if (h < 24) {
						return h + "小时 前";
					} else {
						if (d < 7) {
							return d + "天 前";
						} else {
							if (z < 3) {
								return z + "周 前";
							} else {
								return time1.toString();
							}
						}
					}
				}
			}
		} else {
			return time2.toString();
		}
	}

	public static List<SearchResult> formatTopicRe(ResultSet rs) {
		List<SearchResult> list = new ArrayList<SearchResult>();
		SearchResult sr;
		try {
			sr = new SearchResult();
			while (rs.next()) {
				sr.setReId(rs.getInt("ID"));
				sr.setReTitle(rs.getString("Body"));
				sr.setReEditorId(rs.getInt("UserID"));
				sr.setReEditor(rs.getString("CreatorName"));
				sr.setReHitNum(rs.getInt("Hit"));
				sr.setReCreatOn(rs.getTimestamp("CreatedOn").toString());
				list.add(sr);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static List<SearchResult> formatBlogRs(ResultSet rs) {
		List<SearchResult> list = new ArrayList<SearchResult>();
		SearchResult sr;
		try {
			sr = new SearchResult();
			while (rs.next()) {
				sr.setReId(rs.getInt("blogId"));
				sr.setReTitle(rs.getString("blogName"));
				sr.setReEditorId(rs.getInt("userId"));
				sr.setReEditor(rs.getString("userNickName"));
				sr.setReHitNum(rs.getInt("readNum"));
				sr.setReCreatOn(rs.getString("time"));
				list.add(sr);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

	public static Date getTodayStart(int addDay) {
		Date s = new Date();
		Calendar currentDate = new GregorianCalendar();
		currentDate.set(Calendar.HOUR_OF_DAY, 0);
		currentDate.set(Calendar.MINUTE, 0);
		currentDate.set(Calendar.SECOND, 0);
		s = ((Date) currentDate.getTime().clone());
		if (addDay != 0) {
			s.setDate(addDay + s.getDate());
		}
		return s;
	}

	public static Date getTodayEnd(int addDay) {
		Date s = new Date();
		Calendar currentDate = new GregorianCalendar();
		currentDate.set(Calendar.HOUR_OF_DAY, 0);
		currentDate.set(Calendar.MINUTE, 0);
		currentDate.set(Calendar.SECOND, 0);
		s = ((Date) currentDate.getTime().clone());
		if (addDay != 0) {
			s.setDate(addDay + s.getDate());
		}
		return s;
	}

	public static Map<String, String> getMapFromStringArray(String[] a,
			String[] b) {
		Map<String, String> map = new HashMap<String, String>();
		for (int i = 0; i < a.length && i < b.length; i++) {
			map.put(a[i], b[i]);
		}
		return map;
	}

	public static Map<Integer, String> getMapFromMap(Map<String, Integer> map,
			Map<String, String> map2) {
		Map<Integer, String> result = new HashMap<Integer, String>();
		Iterator<Entry<String, Integer>> it = map.entrySet().iterator();
		
		while (it.hasNext()) {
			Entry<String, Integer> entry = it.next();
//			System.out.println("map DB" + entry.getKey());
			Iterator<Entry<String, String>> it2 = map2.entrySet().iterator();
			while (it2.hasNext()) {
				Entry<String, String> entry2 = it2.next();
//				System.out.println("来自DB" + entry.getKey() + "来自web"
//						+ entry2.getKey());
				if (entry.getKey().equals(entry2.getKey())) {
					System.out.println("id " + entry.getValue() + " desc"
							+ entry2.getValue());
					result.put(entry.getValue(), entry2.getValue());
				}
			}
		}
		return result;
	}
	public static String getStringFromList(List<?> list,String mark){
		StringBuilder result=new StringBuilder();
		if(list.size()==0){
			return "";
		}else if(list.size()==1){
			return list.get(0).toString();
		}else{
			for(int i =0;i<list.size();i++){
				result.append(list.get(i).toString()+mark);
			}
			result.deleteCharAt(result.length()-1);
			return result.toString();
		}
	}
}
